package com.paipaideli.common.views;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paipaideli.R;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 倒计时控件
 */

public class DownTextView extends LinearLayout {

    private static final String TAG = "CountdownTextView";
    private long mSeconds;
    private final int what_count_down_tick = 1;
    private TextView mHour;
    private TextView mMinute;
    private TextView mSecond;
    private TextView tv_start_end;
    private LinearLayout lin_time;

    private Handler mHandler;

    private int type = 0; //0 距开始 1 距结束 2 已结束
    private long startTime = 0; //0 距开始 1 距结束 2 已结束
    private long endTime = 0; //0 距开始 1 距结束 2 已结束
    private LinearLayout lin_bg;

    public interface CouponDownInterface {
        void runDown();

        void runText(long timer, int type);
    }

    public CouponDownInterface couponDownInterface;


    public DownTextView(Context context) {
        super(context);
        initView(context);
    }

    public DownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public DownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.down_layout, this);
        mHour = (TextView) findViewById(R.id.tv_hour);
        mMinute = (TextView) findViewById(R.id.tv_minute);
        mSecond = (TextView) findViewById(R.id.tv_second);
        tv_start_end = (TextView) findViewById(R.id.tv_start_end);
        lin_bg = findViewById(R.id.lin_bg);
        lin_time = findViewById(R.id.lin_time);

        if (mHandler == null) {
            mHandler = new MyHandler(this);
        }
    }

    private long initSecondType(long seconds, long endSeconds, int type) {
        switch (type) {
            case 0:
                seconds = seconds;
                tv_start_end.setText("距开始：");
                lin_bg.setBackgroundResource(R.color.color_219c00);
                break;
            case 1:
                seconds = endSeconds;
                tv_start_end.setText("距结束：");
                lin_bg.setBackgroundResource(R.color.red);
                break;
            case 2:
                seconds = 0;
                tv_start_end.setText("已结束：");
                lin_bg.setBackgroundResource(R.color.home_gray);
                break;
        }
        return seconds;
    }

    /**
     * @param seconds 开始时长 endSeconds 结束时长
     */
    public void init(final long seconds, long endSeconds, int types, CouponDownInterface couponDownInterface) {
        this.type = types;
        this.endTime = endSeconds;
        this.startTime = seconds;

        long time = initSecondType(seconds, endSeconds, type);
        this.couponDownInterface = couponDownInterface;

        mSeconds = time / 1000; //设置总共的秒数
        if (mSeconds > 0) {
            mHandler.sendEmptyMessageDelayed(what_count_down_tick, 0);
            mSeconds--;
        }
    }


    private static class MyHandler extends Handler {
        private WeakReference<DownTextView> mContext;

        public MyHandler(DownTextView activity) {
            mContext = new WeakReference<DownTextView>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            DownTextView brandActivity = mContext.get();
            if (mContext != null && brandActivity != null) {
                brandActivity.Message(msg.what);
            }
        }
    }

    public void cancel(){
        if (mHandler != null){
            mHandler.removeMessages(what_count_down_tick);
        }
    };

    public void Message(int msg) {
        switch (msg) {
            case what_count_down_tick:
                second2TimeSecond(mSeconds);
                break;
        }
    }

    @Override
    public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
        Log.e(TAG, "removeOnLayoutChangeListener");
        super.removeOnLayoutChangeListener(listener);
    }

    @Override
    public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
        Log.e(TAG, "removeOnAttachStateChangeListener");
        super.removeOnAttachStateChangeListener(listener);
    }

    /**
     * 转化为 hh:mm:ss 格式
     *
     * @param second
     * @return
     */
    private void second2TimeSecond(long second) {
        long hours = second / 3600;
        long minutes = (second % 3600) / 60;
        long seconds = second % 60;

        String hourString = "";
        String minuteString = "";
        String secondString = "";
        if (hours < 10) {
            hourString = "0";
            if (hours == 0) {
                hourString += "0";
            } else {
                hourString += String.valueOf(hours);
            }
        } else {
            hourString = String.valueOf(hours);
        }
        if (minutes < 10) {
            minuteString = "0";
            if (minutes == 0) {
                minuteString += "0";
            } else {
                minuteString += String.valueOf(minutes);
            }
        } else {
            minuteString = String.valueOf(minutes);
        }
        if (seconds < 10) {
            secondString = "0";
            if (seconds == 0) {
                secondString += "0";
            } else {
                secondString += String.valueOf(seconds);
            }
        } else {
            secondString = String.valueOf(seconds);
        }

        mHour.setText(hourString);
        mMinute.setText(minuteString);
        mSecond.setText(secondString);

        switch (type) {
            case 0:
                tv_start_end.setText("距开始：");
                lin_bg.setBackgroundResource(R.color.color_219c00);
                break;
            case 1:
                tv_start_end.setText("距结束：");
                lin_bg.setBackgroundResource(R.color.red);
                break;
            default:
                tv_start_end.setText("已结束：");
                lin_time.setVisibility(GONE);
                lin_bg.setBackgroundResource(R.color.home_gray);
                break;
        }
        /*
         * 倒计时已经走完
         * */
        if (hourString.equals(minuteString) && hourString.equals(secondString) && hourString.equals("00") && type == 2) {
            tv_start_end.setText("距结束：");
            lin_bg.setBackgroundResource(R.color.home_gray);
            couponDownInterface.runDown();
        } else if (hourString.equals(minuteString) && hourString.equals(secondString) && hourString.equals("00")) { //倒计时为0
            switch (type) {
                case 0:
                    type = 1;
                    long l = initSecondType(startTime, endTime, type);
                    mSeconds = l / 1000; //设置总共的秒数
                    mHandler.sendEmptyMessageDelayed(what_count_down_tick, 1000);
                    mSeconds--;
                    couponDownInterface.runText(second * 1000, type);
                    break;
                case 1:
                    type = 2;//最后走完显示数据
                    lin_bg.setBackgroundResource(R.color.home_gray);
                    couponDownInterface.runDown();
                    break;
                case 2:
                    break;
            }
        } else {
            mHandler.sendEmptyMessageDelayed(what_count_down_tick, 1000);
            mSeconds--;
            couponDownInterface.runText(second * 1000, type);
        }
    }

}